package tests;
import java.util.Iterator;
import java.util.Vector;

import junit.framework.TestCase;
import objects.User;
import DB.DataBase;
import Domain.forumManager.ForumController;
import Domain.stompProtocol.ForumFrame;

public class ForumControllerTest extends TestCase {
	DataBase d=new DataBase();
ForumController f=new ForumController(d);

	public void testForumController() {
		assertNotNull(f);
		
	}

	private boolean searchForName(Vector<User> u,String name)
	{
		for (Iterator iterator = u.iterator(); iterator.hasNext();)
		{
			User user = (User) iterator.next();
			if (user.getUserName().equals(name))
				return true;
		}
		return false;
	}
	public void testAddFreind() {
		
		User a=new User();
		a.setUserName("Asaf");
		User b=new User();
		b.setUserName("Dudi");
		f.addUser(a);
		f.addUser(b);
		f.addFreind("Asaf", "Dudi");
		//System.err.println
		//assertTrue(f.getFriendsOfUser("Asaf").contains(b));
		assertTrue(searchForName(f.getFriendsOfUser("Asaf"),"Dudi"));
		//assertEquals((f.getFriendsOfUser("Asaf").elementAt(0).getUserName()),"Dudi");
		//System.err.println
		//(f.getFriendsOfUser("Dudi"));
		//assertNull(f.getFriendsOfUser("Dudi"));
		//assertNull(f.getFriendsOfUser("Asaf"));
		//assertEquals(f.getFriendsOfUser("Dudi"), actual)
		//assertTrue(f.getFriendsOfUser("Dudi").contains(a));
		//assertTrue(f.getFriendsOfUser("Asaf").contains(b));
		
	}

	public void testRemoveFriend() {
		//User a=new User();
		//a.setUserName("Asaf");
		//User b=new User();
		//b.setUserName("Dudi");
		//f.addUser(a);
		//f.addUser(b);
		//f.addFreind("Asaf", "Dudi");
		f.removeFriend("Asaf", "Dudi");
		//assertFalse(f.getFriendsOfUser("Asaf").contains(b));
		assertFalse(searchForName(f.getFriendsOfUser("Asaf"),"Dudi"));
	}
	
	public void testCreateUser() {
		f.createUser("Reuven");
		
		assertEquals(f.getUserByName("Reuven").getUserName(), "Reuven");
		f.createUser("Reuven");  //I have changed your func that return -1 in case user exist
		f.createUser("Reuven");
		System.err.println(f.getAllUsers());
	}
	
	//================fail=====================
	
	public void testRemoveUserString() {
		System.err.println("testRemoveUserString()");
		System.err.println(f.getUserByName("Asaf"));
		int ans=f.removeUser("Asaf");
		if (ans==0)
		{
			System.err.println("Problem");
		}
		else System.err.println(ans ); 
		
		System.err.println	(f.getUserByName("Asaf"));
		System.err.println(f.getAllUsers());
		assertFalse(f.getUserByName("Asaf").isActive());
	}
	
	
		
	public void testRemoveUserUser() {
		System.err.println("testRemoveUserUser()");
		User a=new User();
		a.setUserName("BB");
		f.addUser(a);
		f.removeUser(a);
		System.err.println(f.getAllUsers());
		assertFalse(f.getUserByName("BB").isActive());
	}

	
	
	public void testAddForum() {
		//User a=new User("Asaf");
		//f.addUser(a);
		
		assertEquals(f.addForum("Manassen", "Asaf"), ForumFrame.ERRInvalidTypeOfUSer);
		f.changeUserType("Asaf", 1);
		
		assertEquals(f.addForum("Manassen", "Asaf"), 1);
		
	}

	
	public void testAddThread() {
	
		assertEquals
		(f.addThread("Manassen", "Dudi", "mnsn"),-2);
		
		assertEquals(f.addThread("Manassen", "Asaf", "mnsn"),1);
		
		
		assertEquals(f.getThreads("Manassen").get(0).getThreadName(),"mnsn");
		
	}
	
	public void testAddMessage() {
		
		
		assertNotNull(f.addMessage("Manassen", "Asaf", "mnsn", "hello\nhello"));
		assertEquals(f.getMessages("Manassen", "mnsn").get(0).getMessage(), "hello");
	}
	public void testEditMessage(){
		assertEquals(f.editMessage("Asaf", "Manassen", "mnsn", "hello", "Seccess"),1);
		f.addMessage("Manassen", "Dudi", "mnsn", "Dud\nDud");
		System.err.println(f.editMessage("Dudi", "Manassen", "mnsn", "Dud", "Seccess"));
		assertEquals(f.editMessage("Dudi", "Manassen", "mnsn", "Dud", "Seccess"),1);
		
	}
	
}
